package cn.ycc1.order.service.impl;


import cn.ycc1.feign.clients.AccountClient;
import cn.ycc1.feign.clients.StorageClient;
import cn.ycc1.feign.clients.UserClient;
import cn.ycc1.feign.entity.User;
import cn.ycc1.order.entity.Order;
import cn.ycc1.order.mapper.OrderMapper;
import cn.ycc1.order.service.OrderService;
import cn.ycc1.order.vo.OrderVo;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Title:
 * @Author ycc
 * @Date 2023/7/4 16:23
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    AccountClient accountClient;
    @Autowired
    StorageClient storageClient;

    @Autowired
    UserClient userClient;

    @Override
    @GlobalTransactional
    public Long create(Order order) {
        // 创建订单
        orderMapper.insert(order);
        // 扣用户余额
        accountClient.deduct(order.getUserId(), order.getMoney());
        // 扣库存存
        storageClient.deduct(order.getCommodityCode(), order.getCount());
        return order.getId();
    }

    @Override
    public OrderVo getById(Integer id) {
        OrderVo orderVo = orderMapper.getById(id);
        User user = userClient.findByUserId(orderVo.getUserId());
        orderVo.setUsername(user.getUsername());
        return orderVo;
    }

    @Override
    @SentinelResource("goods")
    public void queryGoods() {
        System.out.println("查询商品");
    }
}
